﻿using System.Xml;                   // for class: XmlDocument

namespace iApprove.Common
{
    public class MessageReader
    {
        // Refer Link:  http://www.c-sharpcorner.com/UploadFile/167ad2/read-xml-file-using-xml-reader-in-C-Sharp/
        //              https://support.microsoft.com/en-in/kb/307548
        //              http://stackoverflow.com/questions/5893884/how-to-read-key-value-from-xml-in-c-sharp


        // Unit Test Proj: https://www.youtube.com/watch?v=8YFZBNFm0OM (Continue ... - Infragistics)
        //                 https://www.youtube.com/user/kudvenkat/search?query=unit+test
        //                 https://www.visualstudio.com/en-us/docs/test/developer-testing/getting-started/getting-started-with-developer-testing
        //                 https://www.youtube.com/watch?v=rmUG2GAWFss (Fox Learning)
        //                 http://stackoverflow.com/questions/62625/how-do-you-know-what-to-test-when-writing-unit-tests (theory)
        //                 https://www.youtube.com/watch?v=rW6LvPP4VvA (Kud Venkat) Theory AAA
        //                 http://stackoverflow.com/questions/24965/beginning-tdd-challenges-solutions-recommendations (Recommend Theory)
        //                 http://cantgrokwontgrok.blogspot.in/2008/09/tdd-getting-started-with-test-driven.html

        public static string GetMessage(string XML, string searchTerm)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(XML);

            XmlNodeList nodes = doc.SelectNodes("root/messagekey");
            foreach (XmlNode node in nodes)
            {
                XmlAttributeCollection nodeAtt = node.Attributes;
                if (nodeAtt["name"].Value.ToString() == searchTerm)
                {
                    XmlDocument childNode = new XmlDocument();
                    childNode.LoadXml(node.OuterXml);
                    return childNode.SelectSingleNode("messagekey/value").InnerText;
                }
            }

            return "Unable to find the supplied message key in App Messages";
        }
    }
}
